using System;
using SharePoint.ImportExport.Core;
using SharePoint2003Import;


namespace SharePointImporter.CommandLine
{
	/// <summary>
	/// This is the cmd line class for the SharePoint importer
	/// </summary>
	class Import
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			Importer ImportGlobals = new Importer();
			if(args.Length> 0)
			{
				if(args.Length==1)
				{
					if(args[0]== "/?")
					{
						PrintUsage();
						return;
					}
				}
			else
				{
#if DEBUG
					Console.WriteLine("Attach debugger");
					Console.ReadLine();
#endif
					Parameters p= new Parameters();
					string lastError;
					if(!p.ProcessArgs(args, out lastError))
					{
						Console.WriteLine("Incorrect parameters! "+lastError);
						Console.WriteLine("");
						PrintUsage() ;
						return;
					}
					Console.WriteLine("*******************************************************");
					Console.WriteLine("You are perfoming an import with the following options");
					Console.WriteLine("\t Data file is read from: "+p.DirectoryName);
					Console.WriteLine("\t Import is performed to: "+p.PortalRoot);
					if(p.AllowImportToExistingSites)
						Console.WriteLine("\t Import to existing sites is allowed");
					else
						Console.WriteLine("\t Import to existing sites is not allowed");
					if(p.AllowImportToExistingLibraries)
						Console.WriteLine("\t Import to existing document libraries is allowed");
					else
						Console.WriteLine("\t Import to existing document libraries is not allowed");
					if(p.ImportDesign)
						Console.WriteLine("\t Importing the design");
					else
						Console.WriteLine("\t Not importing the design");

					Console.WriteLine();

					Console.WriteLine("*******************************************************");


				
					Logger.Init("import" + DateTime.Now.ToString("MMddHHmm") + ".log",p.ReportLevel);
					Logger.LogDataSendingEvent += new Logger.LogDataEventHandler(IncomingEventLogData);

					if(ImportGlobals.Init())
					{
						
						if(p.CheckSchema)
							ImportGlobals.ValidateFiles(p.DirectoryName,p.CreateListTemplates);
						else
							ImportGlobals.Process(p.DirectoryName,p.PortalRoot,p.AllowImportToExistingSites,p.AllowImportToExistingLibraries,p.SetSecurity,p.CreateListTemplates,p.ImportDesign,p.UpdateListItems);
					}
					Logger.LogDataSendingEvent -= new Logger.LogDataEventHandler(IncomingEventLogData);
				}
			}
		else
			{
				PrintUsage();
				return;
			}

		}
		private static void PrintUsage()
		{
			Console.WriteLine("---");
			Console.WriteLine("Usage:");
			Console.WriteLine("-folder x:\\folder");
			Console.WriteLine("-root http://server");
			Console.WriteLine("");
			Console.WriteLine("or");
			Console.WriteLine("-folder x:\\folder");
			Console.WriteLine("-ValidateXML");
			Console.WriteLine("");
			Console.WriteLine("Optional:");
			Console.WriteLine("-CreateListTemplates");
			Console.WriteLine("-OKToImportToExistingSite");
			Console.WriteLine("-OKToImportToExistingLibrary");
			Console.WriteLine("-ImportSecurity");
			Console.WriteLine("-ImportDesign");
			
			Console.WriteLine("-reportLevel Information|Warning|Error");
			Console.WriteLine("");
			Console.WriteLine("Press any key to continue...");
			Console.ReadLine();
		}
	private static void IncomingEventLogData(object sender, SPEventArgs e)
{
		string logArgs = DateTime.Now.ToLongTimeString()+ ":  " + e.Message;
		Console.WriteLine(logArgs);

}

}
}
